﻿using FastCloud.Base;
using FastCloud.Base.Config;
using FastCloud.Base.Desgin;
using FastCloud.Extend.DB;
using FastCloud.Extend.Entity;
using FastCloud.ORG.DB;
using FastCloud.ORG.Entity;
using FastIOC.Attribute;
using FastORM.Entity;
using FastUtil.Convert;
using FastUtil.Data;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;

namespace FastCloud.Controllers.Extend
{
    [Route("frame/extend/element")]
    public class ElementController : BaseController
    {
        [AutoWired]
        private DB_Element DB_Element;
        [AutoWired]
        private DB_ElementRight DB_ElementRight;
        [AutoWired]
        private DB_UserRole DB_UserRole;
        [AutoWired]
        private TokenUtil TokenUtil;

        [Route("list")]
        public IActionResult List()
        {
            return View("~/Views/Extend/Element/Element_List.cshtml");
        }
        [Route("add")]
        public IActionResult Add()
        {
            return View("~/Views/Extend/Element/Element_Add.cshtml");
        }
        [Route("edit")]
        public IActionResult Edit()
        {
            return View("~/Views/Extend/Element/Element_Edit.cshtml");
        }

        [Route("oplist"), HttpPost]
        public IActionResult OpList()
        {
            string ElementName = ValueConvert.ConvertToString(GetFormParam("ElementName"));
            string sql = "1=1";
            List<ParamEntity> ParamList = new List<ParamEntity>();
            if (!string.IsNullOrEmpty(ElementName))
            {
                sql += " and ElementName like @ElementName";
                ParamList.Add(new ParamEntity() { ParamName = "ElementName", ParamValue = "%" + ElementName + "%" });
            }

            return Result.OK(DB_Element.SelectPage("*", sql, "SortNum desc", ParamList));
        }

        [Route("findMainPageElementList"), HttpPost]
        public IActionResult FindMainPageElementList()
        {
            string sql = "1=1";
            List<ParamEntity> ParamList = new List<ParamEntity>();
            List<Frame_Element> ElementList = DB_Element.SelectAllList("*", sql, ParamList);
            List<Frame_ElementRight> RightList = DB_ElementRight.SelectAll();
            TokenEntity Entity = TokenUtil.GetTokenEntity();
            List<Frame_UserRole> UserRoleList = DB_UserRole.SelectAll().Where(a => a.UserGuid == Entity.UserGuid).ToList();
            List<string> RoleGuidList = UserRoleList.Select(a => a.RoleGuid).ToList();
            List<Frame_ElementRight> RightFilterList = RightList.Where(a =>
                (a.RightGuid == Entity.OUGuid && a.RightType == (int)FastCloud.Base.Enum.ORG.RightType.部门) ||
                (a.RightGuid == Entity.UserGuid && a.RightType == (int)FastCloud.Base.Enum.ORG.RightType.人员) ||
                (RoleGuidList.Contains(a.RightGuid) && a.RightType == (int)FastCloud.Base.Enum.ORG.RightType.角色)).ToList();
            List<string> ElementGuidList = RightFilterList.Select(a => a.ElementGuid).ToList();
            ElementList = ElementList.Where(a => ElementGuidList.Contains(a.ElementGuid) || a.IsPublic == (int)FastCloud.Base.Enum.ORG.IsPublic.完全公开).ToList();
            foreach (Frame_Element Element in ElementList)
            {
                Element.TemplatContent = FileUtil.ReadFileContent(CloudUtil.GetContentPath() + "/" + Element.ElementTemplatePath);
            }
            return Result.OK(ElementList);
        }

        [Route("opinsert"), HttpPost]
        public IActionResult OpInsert(Frame_Element Entity)
        {
            DB_Element.Insert(Entity);
            return Result.OK();
        }

        [Route("opupdate"), HttpPost]
        public IActionResult OpUpdate(Frame_Element Entity)
        {
            DB_Element.UpdateByID(Entity);
            return Result.OK();
        }

        [Route("updateElementStatus"), HttpPost]
        public IActionResult UpdateElementStatus()
        {
            string ElementGuid = ValueConvert.ConvertToString(GetFormParam("ElementGuid"));
            int ElementStatus = ValueConvert.ConvertToInt32(GetFormParam("ElementStatus"));
            Frame_Element Entity = DB_Element.SelectOne(ElementGuid);
            Entity.Enable = ElementStatus;
            DB_Element.UpdateByID(Entity);
            return Result.OK();
        }


        [Route("opdetail"), HttpPost]
        public IActionResult OpDetail()
        {
            string ElementGuid = ValueConvert.ConvertToString(GetFormParam("ElementGuid"));
            return Result.OK(DB_Element.SelectOne(ElementGuid));
        }

        [Route("opbatchdelete"), HttpPost]
        [Transitional(TransitonLevel = IsolationLevel.ReadUncommitted)]
        public virtual IActionResult OpBatchDelete()
        {
            string KeyID = ValueConvert.ConvertToString(GetFormParam("KeyID"));
            List<string> KeyList = KeyID.TrimEnd(';').Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries).ToList();
            DB_Element.DeleteBatch(KeyList);
            foreach (var item in KeyList)
            {
                DB_ElementRight.DeleteByElementGuid(item);
            }
            return Result.OK();
        }

        [Route("batchUpdateElementPublicSetting"), HttpPost]
        [Transitional(TransitonLevel = IsolationLevel.ReadUncommitted)]
        public virtual IActionResult BatchUpdateElementPublicSetting()
        {
            string ElementRightStr = ValueConvert.ConvertToString(GetFormParam("ElementRightStr"));
            JArray ModuleRightArray = (JArray)JsonConvert.DeserializeObject(ElementRightStr);
            foreach (JObject item in ModuleRightArray)
            {
                string ElementGuid = ValueConvert.ConvertToString(item.GetValue("ElementGuid"));
                int IsPublic = ValueConvert.ConvertToInt32(item.GetValue("IsPublic"));
                DB_Element.UpdateElementPublic(ElementGuid, IsPublic);
            }
            return Result.OK();
        }

    }
}
